<?php

class Default_Model_AclResourcesView extends Default_Model_Doctrine_BaseAclResourcesView
{


    public function getById($id)
    {
        $rowSet = $this->find($id);
        return $rowSet->current();
    }

    /**
     * 取得所有群組
     *
     * @param string $parent_id
     * @return Zend_Table_Rows
     */
    public function getResource($parent_id='')
    {
        if (empty($parent_id))
        $q = Doctrine_Query::create()
        ->from('Default_Model_AclResourcesView')
        ->where('parent_id IS NULL');
        else
        $q = Doctrine_Query::create()
        ->from('Default_Model_AclResourcesView')
        ->where('parent_id=?', $parent_id);

        $q->orderBy('sort');
        return $q->fetchArray();

    }

    /**
     * 取得模組分類
     *
     */
    public function getResourceKind()
    {
        $q = Doctrine_Query::create()
        ->from('Default_Model_AclResourcesView')
        ->where('kind=?','menu')
        ->orderBy('sort');

        return $q->fetchArray();
    }


    public function getAllResource()
    {
        $arr = $this->_recursiveResource();
        return $arr;
    }

    /**
     * 取得同一階層-子層數量
     *
     * @param string $id
     * @return array
     */
    public function getChildCount($id='')
    {
        $q = Doctrine_Query::create()
        ->select('a.parent_id, COUNT(*) AS cc')
        ->from('Default_Model_AclResources a ');

        if ($id === NULL)
        $q->where ('a.parent_id IN (SELECT b.resource_id FROM Default_Model_AclResources  b WHERE b.parent_id IS NULL)');
        elseif ($id<>'')
        $q->where ('a.parent_id IN (SELECT b.resource_id FROM Default_Model_AclResources b WHERE b.parent_id=?)',$id);
        $q->groupBy('a.parent_id');

        $res = $q->fetchArray();
        $arr = array();
        foreach($res as $val)
        {
            $arr[$val['parent_id']] = $val['cc'];
        }

        return $arr;
    }

    public function getChildArr($id)
    {
        return $this->_getChild($id);
    }

    /**
     * 取得子資源
     *
     * @param unknown_type $id
     * @param unknown_type $arr
     * @return unknown
     */
    private function _getChild($id,&$arr=array())
    {
        $q = Doctrine_Query::create()
        ->select('a.resource_id')
        ->from('Default_Model_AclResources a')
        ->where('a.parent_id=?',$id);
        $res = $q->fetchArray();

        foreach($res as $val)
        {
            $arr[] = $val['resource_id'];
            $this->_getChild($val['resource_id'],$arr);

        }
        return $arr;
    }

    /**
     * 取得父層階層
     *
     * @param string $id
     * @return
     */
    public function getParents($id='')
    {
        return (array)$this->_recursiveResource($id);
    }

    public function getModuleMenu($id='',$topTitle='')
    {
        return $this->_recursiveMenu($id, $topTitle);

    }




    public function getMenuSplit($split='&nbsp;&nbsp;&nbsp;')
    {
        return $this->_recursiveMenu('','',$split);
    }

    public function getMaxSort($id)
    {
        if ($id=='')
        $q = Doctrine_Query::create()
        ->select('MAX(a.sort) AS mm' )
        ->from('Default_Model_AclResourcesView a')
        ->where('parent_id IS NULL');
        else
        $q = Doctrine_Query::create()
        ->select('MAX(a.sort) AS mm' )
        ->from('Default_Model_AclResourcesView a')
        ->where('parent_id=?',$id);
        $res =  $q->fetchOne();

        return $res['mm'];
    }

    /**
     * 取得瀏覽選單
     */
    public function getNavMenu($id)
    {
        $arr = array();

        if ($id <>'')
        $this->_recursivenavMenu($arr,$id);

        return $arr;

    }

    private function _recursivenavMenu(&$arr,$id)
    {

        $q = Doctrine_Query::create()
        ->from('DEFAULT_MODEL_AclResourcesView a')
        ->where('a.resource_id=?',$id);

        $res = $q->fetchOne();

        if ($res['parent_id']<>''){
            $this->_recursivenavMenu(&$arr,$res['parent_id']);
        }
        $arr[$res['resource_id']]= $res['title'];
        return $arr;
    }

    private function _recursiveMenu($id,$parent_title='root',$split='')
    {
        if ($id =='')
        $q = Doctrine_Query::create()
        ->from('DEFAULT_MODEL_AclResourcesView a')
        ->where("a.kind='menu' AND a.parent_id IS NULL")
        ->orderBy('a.sort');
        else
        $q = Doctrine_Query::create()
        ->from('DEFAULT_MODEL_AclResourcesView a')
        ->where("a.kind='menu' AND a.parent_id='$id'")
        ->orderBy('a.sort');
        $res = $q->fetchArray();

        if (count($res)==0) return false;

        foreach ($res as $val){
            if ($split){
                $title = $split.$parent_title;
                $val['title']= $split.$parent_title.$val['title'];
                $arr[$val['resource_id']] = $val;
            }
            else {
                $title = $parent_title.'/'.$val['title'];
                $val['title']= $title;
                $arr[$val['resource_id']] = $val;
            }
            $temp =  $this->_recursiveMenu($val['resource_id'],$title,$split);
            if ($temp)
            $arr = array_merge($arr,$temp);
        }
        return $arr;
    }

    private function _recursiveResource($id='')
    {
        if ($id =='')
        $q = Doctrine_Query::create()
        ->from('Default_Model_AclResourcesView')
        ->where('kind=?','menu')
        ->andWhere('parent_id IS NULL')
        ->orderBy('sort');
        else
        $q = Doctrine_Query::create()
        ->from('Default_Model_AclResourcesView ')
        ->where("parent_id=?",$id)
        ->orderBy('sort');
        $res = $q->fetchArray();

        if (count($res)==0) return false;
        foreach ($res as $val){

            // 模組的話,加入 controller  選單
            if ($val['kind'] == 'module') {
                  $moduleXml  = APPLICATION_PATH.'/modules/'.$val['resource_id']. '/module.xml';
                    if (is_file($moduleXml)){
                            $config = new Zend_Config_Xml($moduleXml);
                            $val['menu'] = $config->menu->item->toArray();
                    }
            }
            $arr[$val['id']] = $val;
            $temp =  $this->_recursiveResource($val['resource_id']);
            if ($temp) {
                $arr [$val['id']]['children']=$temp;
                $arr [$val['id']]['children_count']=count($temp);
            }
            else {

                $arr [$val['id']]['children']=array();
                $arr [$val['id']]['children_count']=0;
            }
        }
        return $arr;
    }


}